#include "Stack.h"


void STInit(ST* st)
{
	assert(st);
	st->arr = NULL;
	st->capacity = st->size = 0;
}

void STDstory(ST* st)
{
	if (st->arr)
	{
		free(st->arr);
		st->arr = NULL;
	}
	st->capacity = st->size = 0;
}

void STPush(ST* st, STDatatype x)
{
	assert(st);
	if (st->capacity == st->size)
	{
		int newcapacity = st->capacity == 0 ? sizeof(STDatatype) : st->capacity * 2;
		STDatatype* ret = (STDatatype*)realloc(st->arr,sizeof(STDatatype)*newcapacity);
		if (ret == NULL)
		{
			perror("realloc");
			return;
		}
		st->capacity = newcapacity;
		st->arr = ret;
	}
	st->arr[st->size] = x;
	st->size++;
}

int STEmpty(ST* st)
{
	assert(st);
	return st->size == 0;
}

int STTop(ST* st)
{
	assert(!STEmpty(st));
	return st->arr[st->size - 1];
}

void STPop(ST* st)
{
	assert(st);
	st->arr[st->size--];
}
